<?php

namespace app\admin\controller;

use app\common\controller\Backend;

/**
 * 统计图表示例
 *
 * @icon   fa fa-charts
 * @remark 展示在FastAdmin中使用Echarts展示丰富多彩的统计图表
 */
class Echarts extends Backend
{
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();

    }

    /**
     * 查看
     */
    public function index()
    {
        if(!$this->auth->isSuperAdmin()){
            $stand_id = $this->auth->stand_id;
            $stand = \app\common\model\Stand::field('id,name')->where('id',$stand_id)->select();
        }else{
            $stand_id = input('stand_id') ?? 1;
            $stand = \app\common\model\Stand::field('id,name')->select();
        }

        //查询最近一年的时间戳
        $dateArray = [];
        $currentDate = new \DateTime();
        $cprk = [];//打包数量
        for ($month = 11; $month >= 0; $month--) {
            $targetDate = clone $currentDate;
            $targetDate->modify("-$month month");
            $firstDayOfMonth = $targetDate->modify('first day of this month');
            $to_month = $targetDate->format('Y-m');
            $dateArray[] = $to_month;
            $start_time = strtotime($to_month);
            $end_time = strtotime(  ' +1 month',$start_time)-1;
            //当月打包数量
            $cprk[] = \app\common\model\Cprk::where('createtime','between time',[$start_time,$end_time])->where('stand_id',$stand_id)->sum('num');
        }
        //获取所有站点

        $this->view->assign('stand', $stand);
        $this->view->assign('stand_id', $stand_id);
        $this->assignconfig('dateArray', $dateArray);
        $this->assignconfig('cprk', $cprk);
        return $this->view->fetch();
    }


}
